package com.bitejiuyeke.forum.services.impl;

import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import com.bitejiuyeke.forum.dao.BoardMapper;
import com.bitejiuyeke.forum.exception.ApplicationException;
import com.bitejiuyeke.forum.model.Board;
import com.bitejiuyeke.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if (num <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao查询数据库中的数据
        List<Board> result = boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));

        }
       return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));

        }
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 打印日志
            log.warn("{}, board id = {}", ResultCode.ERROR_IS_NULL.toString(), id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        //更新帖子数量  重新创建一个Board对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        //调用DAO  更新
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            //打印日志
            log.warn("{}, 受影响的行数不等于1.", ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询板块详情
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn("{} , board id = {}", ResultCode.FAILED_BOARD_NOT_EXISTS.toString(), id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //判断减1 之后是否小于0
       if (updateBoard.getArticleCount() < 0) {
           //如果小于0 那么设置为0
           updateBoard.setArticleCount(0);
       }
        //调用DAO  更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
       if (row != 1) {
           log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于1 .");
           throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
       }


    }
}
